//
//  DLMachineVC.m
//  delongzhitou
//
//  Created by Mr.Wang on 2018/11/22.
//  Copyright © 2018 delongzhitou. All rights reserved.
//

#import "DLMachineVC.h"
#import "DLMachineTVHeaderView.h"
#import "DLNewsFlashView.h"

@interface DLMachineVC ()

@property (nonatomic, strong) UIView *falseNavView;

@property (nonatomic, strong) DLMachineTVHeaderView *headView;

@end

@implementation DLMachineVC

- (void) networkRetryyyy {
    
}
- (void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setUpNav];
//    [self checkupAppUpdate];
}
- (void) viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    kuserManager.currentTabIndex = 0;
}
- (void) viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
//    [self.headView.newsFlashView.iFlySpeechSynthesizer stopSpeaking];
//    kuserManager.homeCurrentSpeakingText = @"";
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self checkupAppUpdate];
    
    // Do any additional setup after loading the view.
    [self layoutSubControls];
}

- (void) layoutSubControls {
    
    UIView *bgView = [UIView new];
    bgView.frame = CGRectMake(0, 0, self.view.width, self.view.height);
    bgView.backgroundColor = kColorRGB(230, 235, 240);
    [self.view addSubview:bgView];
    
    UIImageView *falseNavView = [UIImageView new];
    falseNavView.frame = CGRectMake(0, 0, kScreenWidth, kHeightNavigation);
    falseNavView.image = [UIImage imageNamed:@"normal_navbg"];
    [bgView addSubview:falseNavView];
    falseNavView.userInteractionEnabled = YES;
    self.falseNavView = falseNavView;
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.frame = CGRectMake(kScreenWidth / 3, kHeightNavigationStatusBar, falseNavView.width / 3, kHeightNavigation - kHeightNavigationStatusBar);
    titleLabel.text = @"智语";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = kSystemFont(18);
    [falseNavView addSubview:titleLabel];
    
    self.plainTableView.frame = CGRectMake(0, falseNavView.bottom, kScreenWidth, kScreenSafeAreaHeight - kHeightTabBar - falseNavView.height);
//    self.plainTableView.backgroundColor = kDLViewNormalBgColor;
    self.plainTableView.backgroundColor = kColorRGB(230, 235, 240);
    self.plainTableView.rowHeight = 0.001;
    self.plainTableView.scrollEnabled = NO;
    [bgView addSubview:self.plainTableView];
    
    self.plainTableView.tableFooterView = [UIView new];
    
}

- (void) checkupAppUpdate {
    
    NSString *postUrl = HTTPCheckAppUpdateInfo;
    NSDictionary *postDic = @{};
    [[DLNetWorkManager shareInstance] requestWithMethod:DLRequestMethodPOST path:postUrl parameters:postDic success:^(id  _Nonnull responseObject) {
        
        if (kRequestSuccess(responseObject)) {
            NSDictionary *resultDic = responseObject[@"result"];
            NSString *isForceUpdating = [NSString stringWithFormat:@"%@", resultDic[@"isForceUpdating"]];
            if (isForceUpdating.integerValue == 0) {
                //不需要更新
                NSLog(@"版本更新：不需要更新-0");
                [self setUpHeaderView];
            } else if (isForceUpdating.integerValue == 1){
                //不强制更新
                NSLog(@"版本更新：不强制更新-1");
                [self setUpHeaderView];
                [self doNotMustUpdate:responseObject];
            } else if (isForceUpdating.integerValue == 2) {
                //强制更新
                NSLog(@"版本更新：强制更新-2");
                [self mustUpdate:responseObject];
            } else {
                //不需要更新
                NSLog(@"版本更新：不需要更新-其他");
                [self setUpHeaderView];
            }
        } else {
            [self setUpHeaderView];
        }
    } failure:^(NSError * _Nonnull error) {
        NSLog(@"请求接口失败，错误是：%@", error);
//        self.showNetworkErrorView = YES;
    }];
}
- (void) mustUpdate:(NSDictionary *)responseObject {
    
    NSDictionary *resultDic = responseObject[@"result"];
    NSString *content = [NSString stringWithFormat:@"%@", resultDic[@"content"]];
    
    //弹出提示框
    UIAlertController *upDateAlert = [UIAlertController alertControllerWithTitle:@"发现新版本" message:kSafeString(content) preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *updateAction = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [self updateNewVersion:responseObject];
    }];
    
    UIAlertAction *doNotUpdateAction = [UIAlertAction actionWithTitle:@"退出" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        exit(0);
    }];
    
    [upDateAlert addAction:doNotUpdateAction];
    [upDateAlert addAction:updateAction];
    [self presentViewController:upDateAlert animated:YES completion:^{
        
    }];
}
- (void) doNotMustUpdate:(NSDictionary *)responseObject {
    
    NSDictionary *resultDic = responseObject[@"result"];
    NSString *content = [NSString stringWithFormat:@"%@", resultDic[@"content"]];
    
    //弹出提示框
    UIAlertController *upDateAlert = [UIAlertController alertControllerWithTitle:@"发现新版本" message:kSafeString(content) preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *updateAction = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        [self updateNewVersion:responseObject];
    }];
    
    UIAlertAction *doNotUpdateAction = [UIAlertAction actionWithTitle:@"暂不更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [upDateAlert addAction:doNotUpdateAction];
    [upDateAlert addAction:updateAction];
    [self presentViewController:upDateAlert animated:YES completion:^{
        
    }];
}
- (void) updateNewVersion:(NSDictionary *)responseObject {
    NSDictionary *resultDic = responseObject[@"result"];
    NSString *downloadPageUrl = [NSString stringWithFormat:@"%@", resultDic[@"downloadPageUrl"]];
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:downloadPageUrl] options:@{} completionHandler:^(BOOL success) {
        
    }];
}
- (void) setUpHeaderView {
    self.headView = [[DLMachineTVHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.plainTableView.width, kScreenSafeAreaHeight - kHeightTabBar - self.falseNavView.height)];
    self.plainTableView.tableHeaderView = self.headView;
}

- (void) setUpNav {
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem new];
    self.navigationController.navigationBarHidden = YES;
    self.navigationController.navigationBar.translucent = YES;
    
    self.titleView.title = @"首页";
}
- (void) machineBtnClick {
    DLMachineDetailVC *machineDetailVC = [DLMachineDetailVC new];
    [kuserManager.lastVc.navigationController pushViewController:machineDetailVC animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
